#include "RegionEditDlg.h"

//(*InternalHeaders(RegionEditDlg)
#include <wx/sizer.h>
#include <wx/stattext.h>
#include <wx/textctrl.h>
#include <wx/font.h>
#include <wx/intl.h>
#include <wx/button.h>
#include <wx/string.h>
#include <wx/combobox.h>
//*)
#include <wx/log.h>
#include <wx/tokenzr.h>

#include "regionlistctrl.h"

//(*IdInit(RegionEditDlg)
const long RegionEditDlg::ID_STATICTEXT1 = wxNewId();
const long RegionEditDlg::ID_COMBOBOX3 = wxNewId();
const long RegionEditDlg::ID_STATICTEXT11 = wxNewId();
const long RegionEditDlg::ID_COMBOBOX2 = wxNewId();
const long RegionEditDlg::ID_STATICTEXT12 = wxNewId();
const long RegionEditDlg::ID_COMBOBOX1 = wxNewId();
const long RegionEditDlg::ID_BUTTON1 = wxNewId();
const long RegionEditDlg::ID_STATICTEXT2 = wxNewId();
const long RegionEditDlg::ID_STATICTEXT4 = wxNewId();
const long RegionEditDlg::ID_STATICTEXT5 = wxNewId();
const long RegionEditDlg::ID_STATICTEXT3 = wxNewId();
const long RegionEditDlg::ID_STATICTEXT7 = wxNewId();
const long RegionEditDlg::ID_STATICTEXT6 = wxNewId();
const long RegionEditDlg::ID_STATICTEXT8 = wxNewId();
const long RegionEditDlg::ID_STATICTEXT9 = wxNewId();
const long RegionEditDlg::ID_STATICTEXT10 = wxNewId();
const long RegionEditDlg::ID_TEXTCTRL1 = wxNewId();
const long RegionEditDlg::ID_TEXTCTRL2 = wxNewId();
const long RegionEditDlg::ID_TEXTCTRL3 = wxNewId();
const long RegionEditDlg::ID_TEXTCTRL4 = wxNewId();
const long RegionEditDlg::ID_TEXTCTRL5 = wxNewId();
const long RegionEditDlg::ID_TEXTCTRL6 = wxNewId();
const long RegionEditDlg::ID_TEXTCTRL7 = wxNewId();
const long RegionEditDlg::ID_TEXTCTRL8 = wxNewId();
const long RegionEditDlg::ID_TEXTCTRL9 = wxNewId();
//*)

BEGIN_EVENT_TABLE(RegionEditDlg,wxDialog)
	//(*EventTable(RegionEditDlg)
	//*)
END_EVENT_TABLE()

RegionEditDlg::RegionEditDlg(RegionListCtrl* parent,wxWindowID id,const wxPoint& pos,const wxSize& size) :
    m_parent(parent)
{
    assert(m_parent != NULL);
	//(*Initialize(RegionEditDlg)
	wxBoxSizer* BoxSizer4;
	wxBoxSizer* BoxSizer2;
	wxBoxSizer* BoxSizer1;
	wxBoxSizer* BoxSizer3;

	Create(parent, wxID_ANY, wxEmptyString, wxDefaultPosition, wxDefaultSize, wxDEFAULT_DIALOG_STYLE, _T("wxID_ANY"));
	SetClientSize(wxSize(374,322));
	BoxSizer1 = new wxBoxSizer(wxHORIZONTAL);
	BoxSizer3 = new wxBoxSizer(wxVERTICAL);
	StaticText10 = new wxStaticText(this, ID_STATICTEXT1, _("国家编码："), wxDefaultPosition, wxSize(108,20), 0, _T("ID_STATICTEXT1"));
	BoxSizer3->Add(StaticText10, 0, wxALL|wxALIGN_LEFT|wxALIGN_BOTTOM, 5);
	m_countryCode = new wxComboBox(this, ID_COMBOBOX3, wxEmptyString, wxDefaultPosition, wxSize(118,22), 0, 0, wxTE_PROCESS_ENTER, wxDefaultValidator, _T("ID_COMBOBOX3"));
	BoxSizer3->Add(m_countryCode, 1, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
	StaticText11 = new wxStaticText(this, ID_STATICTEXT11, _("省份编码："), wxDefaultPosition, wxDefaultSize, 0, _T("ID_STATICTEXT11"));
	BoxSizer3->Add(StaticText11, 0, wxALL|wxALIGN_LEFT|wxALIGN_BOTTOM, 5);
	m_provinceCode = new wxComboBox(this, ID_COMBOBOX2, wxEmptyString, wxDefaultPosition, wxSize(118,22), 0, 0, 0, wxDefaultValidator, _T("ID_COMBOBOX2"));
	BoxSizer3->Add(m_provinceCode, 1, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
	StaticText12 = new wxStaticText(this, ID_STATICTEXT12, _("城市编码："), wxDefaultPosition, wxDefaultSize, 0, _T("ID_STATICTEXT12"));
	BoxSizer3->Add(StaticText12, 0, wxALL|wxALIGN_LEFT|wxALIGN_BOTTOM, 5);
	m_cityCode = new wxComboBox(this, ID_COMBOBOX1, wxEmptyString, wxDefaultPosition, wxSize(118,22), 0, 0, 0, wxDefaultValidator, _T("ID_COMBOBOX1"));
	BoxSizer3->Add(m_cityCode, 1, wxALL|wxSHAPED|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
	m_saveBtn = new wxButton(this, ID_BUTTON1, _("保存"), wxDefaultPosition, wxSize(92,92), 0, wxDefaultValidator, _T("ID_BUTTON1"));
	m_saveBtn->SetForegroundColour(wxColour(0,128,0));
	wxFont m_saveBtnFont(18,wxSWISS,wxFONTSTYLE_NORMAL,wxNORMAL,false,_T("Arial"),wxFONTENCODING_DEFAULT);
	m_saveBtn->SetFont(m_saveBtnFont);
	BoxSizer3->Add(m_saveBtn, 0, wxALL|wxEXPAND|wxSHAPED|wxALIGN_TOP|wxALIGN_CENTER_HORIZONTAL, 5);
	BoxSizer1->Add(BoxSizer3, 2, wxALL|wxEXPAND|wxALIGN_LEFT|wxALIGN_BOTTOM, 5);
	BoxSizer4 = new wxBoxSizer(wxVERTICAL);
	StaticText1 = new wxStaticText(this, ID_STATICTEXT2, _("简体国家："), wxDefaultPosition, wxDefaultSize, 0, _T("ID_STATICTEXT2"));
	BoxSizer4->Add(StaticText1, 1, wxTOP|wxBOTTOM|wxLEFT|wxALIGN_RIGHT|wxALIGN_BOTTOM, 5);
	StaticText3 = new wxStaticText(this, ID_STATICTEXT4, _("繁体国家："), wxDefaultPosition, wxDefaultSize, 0, _T("ID_STATICTEXT4"));
	BoxSizer4->Add(StaticText3, 1, wxTOP|wxBOTTOM|wxLEFT|wxALIGN_RIGHT|wxALIGN_BOTTOM, 5);
	StaticText4 = new wxStaticText(this, ID_STATICTEXT5, _("英语国家："), wxDefaultPosition, wxDefaultSize, 0, _T("ID_STATICTEXT5"));
	BoxSizer4->Add(StaticText4, 1, wxTOP|wxBOTTOM|wxLEFT|wxALIGN_RIGHT|wxALIGN_BOTTOM, 5);
	StaticText2 = new wxStaticText(this, ID_STATICTEXT3, _("简体省份："), wxDefaultPosition, wxDefaultSize, 0, _T("ID_STATICTEXT3"));
	BoxSizer4->Add(StaticText2, 1, wxTOP|wxBOTTOM|wxLEFT|wxALIGN_RIGHT|wxALIGN_BOTTOM, 5);
	StaticText6 = new wxStaticText(this, ID_STATICTEXT7, _("繁体省份："), wxDefaultPosition, wxDefaultSize, 0, _T("ID_STATICTEXT7"));
	BoxSizer4->Add(StaticText6, 1, wxTOP|wxBOTTOM|wxLEFT|wxALIGN_RIGHT|wxALIGN_BOTTOM, 5);
	StaticText5 = new wxStaticText(this, ID_STATICTEXT6, _("英语省份："), wxDefaultPosition, wxDefaultSize, 0, _T("ID_STATICTEXT6"));
	BoxSizer4->Add(StaticText5, 1, wxTOP|wxBOTTOM|wxLEFT|wxALIGN_RIGHT|wxALIGN_BOTTOM, 5);
	StaticText7 = new wxStaticText(this, ID_STATICTEXT8, _("简体城市："), wxDefaultPosition, wxDefaultSize, 0, _T("ID_STATICTEXT8"));
	BoxSizer4->Add(StaticText7, 1, wxTOP|wxBOTTOM|wxLEFT|wxALIGN_RIGHT|wxALIGN_BOTTOM, 5);
	StaticText8 = new wxStaticText(this, ID_STATICTEXT9, _("繁体城市："), wxDefaultPosition, wxDefaultSize, 0, _T("ID_STATICTEXT9"));
	BoxSizer4->Add(StaticText8, 1, wxTOP|wxBOTTOM|wxLEFT|wxALIGN_RIGHT|wxALIGN_BOTTOM, 5);
	StaticText9 = new wxStaticText(this, ID_STATICTEXT10, _("英语城市："), wxDefaultPosition, wxDefaultSize, 0, _T("ID_STATICTEXT10"));
	BoxSizer4->Add(StaticText9, 1, wxTOP|wxBOTTOM|wxLEFT|wxALIGN_RIGHT|wxALIGN_BOTTOM, 5);
	BoxSizer1->Add(BoxSizer4, 1, wxTOP|wxBOTTOM|wxEXPAND|wxALIGN_LEFT|wxALIGN_BOTTOM, 5);
	BoxSizer2 = new wxBoxSizer(wxVERTICAL);
	m_simpleChineseCountry = new wxTextCtrl(this, ID_TEXTCTRL1, wxEmptyString, wxDefaultPosition, wxSize(149,22), 0, wxDefaultValidator, _T("ID_TEXTCTRL1"));
	BoxSizer2->Add(m_simpleChineseCountry, 1, wxALL|wxEXPAND|wxALIGN_LEFT|wxALIGN_BOTTOM, 5);
	m_traditionalChineseCountry = new wxTextCtrl(this, ID_TEXTCTRL2, wxEmptyString, wxDefaultPosition, wxDefaultSize, 0, wxDefaultValidator, _T("ID_TEXTCTRL2"));
	BoxSizer2->Add(m_traditionalChineseCountry, 1, wxALL|wxEXPAND|wxALIGN_LEFT|wxALIGN_BOTTOM, 5);
	m_englishCountry = new wxTextCtrl(this, ID_TEXTCTRL3, wxEmptyString, wxDefaultPosition, wxDefaultSize, 0, wxDefaultValidator, _T("ID_TEXTCTRL3"));
	BoxSizer2->Add(m_englishCountry, 1, wxALL|wxEXPAND|wxALIGN_LEFT|wxALIGN_BOTTOM, 5);
	m_simpleChineseProvince = new wxTextCtrl(this, ID_TEXTCTRL4, wxEmptyString, wxDefaultPosition, wxDefaultSize, 0, wxDefaultValidator, _T("ID_TEXTCTRL4"));
	BoxSizer2->Add(m_simpleChineseProvince, 1, wxALL|wxEXPAND|wxALIGN_LEFT|wxALIGN_BOTTOM, 5);
	m_traditionalChineseProvince = new wxTextCtrl(this, ID_TEXTCTRL5, wxEmptyString, wxDefaultPosition, wxDefaultSize, 0, wxDefaultValidator, _T("ID_TEXTCTRL5"));
	BoxSizer2->Add(m_traditionalChineseProvince, 1, wxALL|wxEXPAND|wxALIGN_LEFT|wxALIGN_BOTTOM, 5);
	m_englishProvince = new wxTextCtrl(this, ID_TEXTCTRL6, wxEmptyString, wxDefaultPosition, wxDefaultSize, 0, wxDefaultValidator, _T("ID_TEXTCTRL6"));
	BoxSizer2->Add(m_englishProvince, 1, wxALL|wxEXPAND|wxALIGN_LEFT|wxALIGN_BOTTOM, 5);
	m_simpleChineseCity = new wxTextCtrl(this, ID_TEXTCTRL7, wxEmptyString, wxDefaultPosition, wxDefaultSize, 0, wxDefaultValidator, _T("ID_TEXTCTRL7"));
	BoxSizer2->Add(m_simpleChineseCity, 1, wxALL|wxEXPAND|wxALIGN_LEFT|wxALIGN_BOTTOM, 5);
	m_traditionalChineseCity = new wxTextCtrl(this, ID_TEXTCTRL8, wxEmptyString, wxDefaultPosition, wxDefaultSize, 0, wxDefaultValidator, _T("ID_TEXTCTRL8"));
	BoxSizer2->Add(m_traditionalChineseCity, 1, wxALL|wxEXPAND|wxALIGN_LEFT|wxALIGN_BOTTOM, 5);
	m_englishCity = new wxTextCtrl(this, ID_TEXTCTRL9, wxEmptyString, wxDefaultPosition, wxDefaultSize, 0, wxDefaultValidator, _T("ID_TEXTCTRL9"));
	BoxSizer2->Add(m_englishCity, 1, wxALL|wxEXPAND|wxALIGN_LEFT|wxALIGN_BOTTOM, 5);
	BoxSizer1->Add(BoxSizer2, 3, wxALL|wxEXPAND|wxSHAPED|wxALIGN_LEFT|wxALIGN_BOTTOM, 5);
	SetSizer(BoxSizer1);
	SetSizer(BoxSizer1);
	Layout();

	Connect(ID_COMBOBOX3,wxEVT_COMMAND_COMBOBOX_SELECTED,(wxObjectEventFunction)&RegionEditDlg::OnCountryCodeSelected);
	Connect(ID_COMBOBOX3,wxEVT_COMMAND_TEXT_UPDATED,(wxObjectEventFunction)&RegionEditDlg::OnCountryCodeSelected);
	Connect(ID_COMBOBOX3,wxEVT_COMMAND_TEXT_ENTER,(wxObjectEventFunction)&RegionEditDlg::OnCountryCodeSelected);
	Connect(ID_COMBOBOX2,wxEVT_COMMAND_COMBOBOX_SELECTED,(wxObjectEventFunction)&RegionEditDlg::OnProvinceCodeSelected);
	Connect(ID_COMBOBOX2,wxEVT_COMMAND_TEXT_UPDATED,(wxObjectEventFunction)&RegionEditDlg::OnProvinceCodeSelected);
	Connect(ID_COMBOBOX2,wxEVT_COMMAND_TEXT_ENTER,(wxObjectEventFunction)&RegionEditDlg::OnProvinceCodeSelected);
	Connect(ID_BUTTON1,wxEVT_COMMAND_BUTTON_CLICKED,(wxObjectEventFunction)&RegionEditDlg::OnSaveBtnClick);
	//*)
}

RegionEditDlg::~RegionEditDlg()
{
	//(*Destroy(RegionEditDlg)
	//*)
}

int RegionEditDlg::InitEditDlg(long indexId)
{
    m_isEditDlg = true;
    m_listIndex = indexId;
    SetTitle(wxString::Format(wxT("编辑地区编码：%s"), m_parent->GetItemText(indexId, 0).c_str()));

    wxStringTokenizer tkz(m_parent->GetItemText(indexId, 0), wxT("_"));

    m_countryCode->SetValue(tkz.GetNextToken());
    m_countryCode->SetEditable(false);
    if( tkz.HasMoreTokens() ) m_provinceCode->SetValue(tkz.GetNextToken());
    m_provinceCode->SetEditable(false);
    if( tkz.HasMoreTokens() ) m_cityCode->SetValue(tkz.GetNextToken());
    m_cityCode->SetEditable(false);

    #define SET_TEXT(column, type) \
        m_simpleChinese##type->SetValue(m_parent->GetItemText(indexId, column)); \
        m_simpleChinese##type->SetEditable(!m_simpleChinese##type->GetValue().empty()); \
        m_traditionalChinese##type->SetValue(m_parent->GetItemText(indexId, column + 1)); \
        m_traditionalChinese##type->SetEditable(!m_traditionalChinese##type->GetValue().empty()); \
        m_english##type->SetValue(m_parent->GetItemText(indexId, column + 2));\
        m_english##type->SetEditable(!m_english##type->GetValue().empty());

    SET_TEXT(1, Country);
    SET_TEXT(4, Province);
    SET_TEXT(7, City);
    #undef SET_TEXT
    return 0;
}

int RegionEditDlg::InitCreateDlg()
{
    m_isEditDlg = false;
    SetTitle(wxT("新建地区"));
    ClearCountry();
    vector<const RegionField*> countries = m_parent->GetManager()->GetCountries();
    for( const RegionField * country: countries )
    {
        m_countryCode->Append(country->GetName(REGION_CODE), (void *)country);
    }
    ClearProvince();
    ClearCity();
    return 0;
}

bool RegionEditDlg::InputLegal()
{
    {
        bool isEmpty = m_countryCode->GetValue().empty();
        if( m_simpleChineseCountry->GetValue().empty() != isEmpty ) return false;
        if( m_traditionalChineseCountry->GetValue().empty() != isEmpty ) return false;
        if( m_englishCountry->GetValue().empty() != isEmpty ) return false;
    }
    return true;
}


void RegionEditDlg::SetCountryEditable(bool isEditable)
{
    m_simpleChineseCountry->SetEditable(isEditable);
    m_traditionalChineseCountry->SetEditable(isEditable);
    m_englishCountry->SetEditable(isEditable);
}

void RegionEditDlg::SetProvinceEditable(bool isEditable)
{
    m_simpleChineseProvince->SetEditable(isEditable);
    m_traditionalChineseProvince->SetEditable(isEditable);
    m_englishProvince->SetEditable(isEditable);
}

void RegionEditDlg::SetCityEditable(bool isEditable)
{
    m_simpleChineseCity->SetEditable(isEditable);
    m_traditionalChineseCity->SetEditable(isEditable);
    m_englishCity->SetEditable(isEditable);
}

void RegionEditDlg::ClearCountry(bool skipCode)
{
    if(!skipCode) m_countryCode->Clear();
    m_simpleChineseCountry->Clear();
    m_traditionalChineseCountry->Clear();
    m_englishCountry->Clear();
    SetCountryEditable(true);
}

void RegionEditDlg::ClearProvince(bool skipCode)
{
    if(!skipCode) m_provinceCode->Clear();
    m_simpleChineseProvince->Clear();
    m_traditionalChineseProvince->Clear();
    m_englishProvince->Clear();
    SetProvinceEditable(true);
}

void RegionEditDlg::ClearCity()
{
    m_cityCode->Clear();
    m_simpleChineseCity->Clear();
    m_traditionalChineseCity->Clear();
    m_englishCity->Clear();
    SetCityEditable(true);
}


void RegionEditDlg::OnSaveBtnClick(wxCommandEvent& event)
{
    if( m_isEditDlg ) SaveEdit();
    else SaveCreate();
}

void RegionEditDlg::SaveEdit()
{
    int ret = 0;
    #define UPDATE_ITEM_TEXT(column, type) \
    if( m_simpleChinese##type->IsEditable() ) \
    { \
        ret |= m_parent->UpdateItemText(m_listIndex, column, m_simpleChinese##type->GetValue()); \
    } \
    if( m_traditionalChinese##type->IsEditable() ) \
    { \
        ret |= m_parent->UpdateItemText(m_listIndex, column + 1, m_traditionalChinese##type->GetValue()); \
    } \
    if( m_english##type->IsEditable() ) \
    { \
        ret |= m_parent->UpdateItemText(m_listIndex, column + 2, m_english##type->GetValue()); \
    }

    UPDATE_ITEM_TEXT(1, Country);
    UPDATE_ITEM_TEXT(4, Province);
    UPDATE_ITEM_TEXT(7, City);
    #undef UPDATE_ITEM_TEXT
    if( ret == 0 ) EndModal(wxID_OK);
}

void RegionEditDlg::SaveCreate()
{
    Region newRegion;
    #define SET_FIELD_NAME(type, codeType) \
    if( m_##english##type->IsEditable() )\
    {\
        wxLogMessage(wxT("new %s"), m_##codeType->GetValue().c_str());\
        newRegion.GetMutable##type()->SetName(SIMPLE_CHINESE, m_simpleChinese##type->GetValue());\
        newRegion.GetMutable##type()->SetName(TRADITIONAL_CHINESE, m_traditionalChinese##type->GetValue());\
        newRegion.GetMutable##type()->SetName(ENGLISH, m_english##type->GetValue());\
        newRegion.GetMutable##type()->SetName(REGION_CODE, m_##codeType->GetValue());\
    }\
    else\
    {\
        long index = m_##codeType->FindString(m_##codeType->GetValue());\
        wxLogMessage(wxT("%ld, %d"), index, ((const RegionField*)m_##codeType->GetClientData(index))->GetId());\
        newRegion.Set##type(*(const RegionField*)m_##codeType->GetClientData(index));\
    }

    //

    SET_FIELD_NAME(Country, countryCode);
    SET_FIELD_NAME(Province, provinceCode);
    SET_FIELD_NAME(City, cityCode);
    #undef SET_FIELD_NAME
    wxLogMessage(wxT("cnid: %d"), newRegion.GetCountry().GetId());
    if( m_parent->GetManager()->CreateField(&newRegion) == 0 ) EndModal(wxID_OK);
}


void RegionEditDlg::OnCountryCodeSelected(wxCommandEvent& event)
{
    ClearProvince();
    ClearCity();
    int index = m_countryCode->GetCurrentSelection();
    if( index == wxNOT_FOUND ) index = m_countryCode->FindString(m_countryCode->GetValue());
    wxLogDebug(wxT("%s, %s"), m_countryCode->GetValue().c_str(), m_countryCode->GetString(index).c_str());
    if( index == wxNOT_FOUND )
    {
        ClearCountry(true);
        return;
    }
    const RegionField * country = (const RegionField *) m_countryCode->GetClientData(index);
    m_simpleChineseCountry->SetValue(country->GetName(SIMPLE_CHINESE));
    m_traditionalChineseCountry->SetValue(country->GetName(TRADITIONAL_CHINESE));
    m_englishCountry->SetValue(country->GetName(ENGLISH));
    SetCountryEditable(false);
    vector<const RegionField *> provinces = m_parent->GetManager()->GetProvinces(country->GetId());
    for( const RegionField * province: provinces )
    {
        m_provinceCode->Append(province->GetName(REGION_CODE), (void *)province);
    }
    //wxMessageBox(name);
}

void RegionEditDlg::OnProvinceCodeSelected(wxCommandEvent& event)
{
    ClearCity();
    int index = m_provinceCode->GetCurrentSelection();
    if( index == wxNOT_FOUND ) index = m_provinceCode->FindString(m_provinceCode->GetValue());
    if( index == wxNOT_FOUND )
    {
        ClearProvince(true);
        return;
    }
    const RegionField * province = (const RegionField *) m_provinceCode->GetClientData(index);
    m_simpleChineseProvince->SetValue(province->GetName(SIMPLE_CHINESE));
    m_traditionalChineseProvince->SetValue(province->GetName(TRADITIONAL_CHINESE));
    m_englishProvince->SetValue(province->GetName(ENGLISH));
    SetProvinceEditable(false);
}
